//
//  CopyController.swift
//  DSP
//
//  Created by tucici on 2024/7/17.
//

import UIKit
import SwiftyJSON
import TCCKit
class CopyController: DSAlertController {
    private var bgView : UIView!
    private var theme : UILabel!
    private var line1 : UIView!
    private var line2 : UIView!
    private var cancel : UIButton!
    private var ok : UIButton!
    private var selectOutput : OutputModel?
    var array = [OutputModel]()
    var block : ((OutputModel)->Void)?
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        initData()
    }
    override func initUI() {
        
        bgView = UIView()
        bgView.bgHexColor = HEX_BG
        bgView.cornerRadius = 10.k_h
        
        theme = UILabel()
        theme.text = "Please select the object of copy"
        theme.font = font(18)
        theme.textHexColor = "#000000"
        
        line1 = UIView()
        line1.bgHexColor = "#000000"
        line2 = UIView()
        line2.bgHexColor = "#000000"
        
        cancel = UIButton(type: .custom)
        cancel.setTitle("Cancel", for: .normal)
        cancel.textHexColor = "#000000"
        cancel.titleLabel?.font = font(16)
        cancel.addTarget(self, action: #selector(actionForButton), for: .touchUpInside)
        
        ok = UIButton(type: .custom)
        ok.setTitle("OK", for: .normal)
        ok.textHexColor = "#000000"
        ok.titleLabel?.font = font(16)
        ok.addTarget(self, action: #selector(actionForButton), for: .touchUpInside)
        
        autoAddCollectionView = false
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .clear
        collectionView.register(InputItem.self)
        
        view.addSubview(bgView)
        bgView.addSubview(theme)
        bgView.addSubview(line1)
        bgView.addSubview(line2)
        bgView.addSubview(cancel)
        bgView.addSubview(ok)
        bgView.addSubview(collectionView)
        
        bgView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(k_w(30))
            make.centerY.equalToSuperview()
            make.height.equalTo(k_h(172))
        }
        
        theme.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.height.equalTo(k_h(40))
            make.top.equalToSuperview()
        }
        
        line1.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(k_h(10))
            make.height.equalTo(1)
            make.top.equalTo(theme.snp.bottom)
        }
        
        collectionView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(line1.snp.bottom)
            make.height.equalTo(k_h(90))
        }
        
        line2.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
            make.top.equalTo(collectionView.snp.bottom)
        }
        
        ok.snp.makeConstraints { make in
            make.right.equalToSuperview().inset(k_w(10))
            make.width.equalTo(40)
            make.top.equalTo(line2.snp.bottom)
            make.bottom.equalToSuperview()
            make.height.equalTo(k_h(40))
        }
        
        cancel.snp.makeConstraints { make in
            make.right.equalTo(ok.snp.left).offset(-k_w(10))
            make.height.equalTo(ok)
            make.centerY.equalTo(ok)
        }
    }
    
    override func initData() {
        collectionView.reloadData()
    }
    
    @objc func actionForButton(sender:UIButton){
        if sender == cancel{
            dismiss(animated: false)
        }else if sender == ok{
            dismiss(animated: false)
            block?(selectOutput ?? OutputModel())
        }
    }
    
}

//MARK: - UICollectionViewDataSource
extension CopyController:UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return array.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.cell(InputItem.self, indexPath) as! InputItem
        let model = array[indexPath.row]
        cell.model = model
        return cell
    }
}

//MARK: - UICollectionViewDelegate
extension CopyController:UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
        let model = array[indexPath.row]
        selectOutput = model.copy() as? OutputModel
        for ch in array{
            ch.select = model.num == ch.num
        }
        collectionView.reloadData()
    }
}

//MARK: - UICollectionViewDelegateFlowLayout
extension CopyController : UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let w = (kUIScreenWidth - k_w(30) * 2.0 - k_w(10) * 6.0) / 4.0
        return CGSize(width: floor(w), height: floor(k_h(30)))
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return k_w(10)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return k_w(10)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: k_w(10), left: k_w(10), bottom: k_w(10), right: k_w(10))
    }
}
